package com.yld.car.market;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;
import com.yld.car.common.ConstantUtils;

public class MoreShareActivity extends BaseActivity {

	private IWXAPI api;
	Runnable runnable = new Runnable() {
		public void run() {
			WXTextObject localWXTextObject = new WXTextObject();
			localWXTextObject.text = "一个找车源的工具，爱车行下载地址：http://www.ichehang.com/search/default.aspx";
			MoreShareActivity.this.wxmsg = new WXMediaMessage();
			MoreShareActivity.this.wxmsg.mediaObject = localWXTextObject;
			MoreShareActivity.this.wxmsg.description = "一个找车源的工具，爱车行下载地址：http://www.ichehang.com/search/default.aspx";
			SendMessageToWX.Req localReq = new SendMessageToWX.Req();
			localReq.transaction = MoreShareActivity.this
					.buildTransaction("text");
			localReq.scene = 0;
			localReq.message = MoreShareActivity.this.wxmsg;
			MoreShareActivity.this.api.sendReq(localReq);
			MoreShareActivity.this.finish();
		}
	};
	private WXMediaMessage wxmsg;

	private String buildTransaction(String paramString) {
		if (paramString == null)
			return String.valueOf(System.currentTimeMillis());
		return paramString + System.currentTimeMillis();
	}

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.more_share);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		api = WXAPIFactory.createWXAPI(this, ConstantUtils.APP_ID, false);
		api.registerApp(ConstantUtils.APP_ID);// 注册分享的应用
		((TextView) findViewById(R.id.title)).setText("推荐“爱车行”给朋友");
		showBackButton();
		Button localButton1 = (Button) findViewById(R.id.shareButton01);
		localButton1.setText("通过短信推荐“爱车行”给朋友");
		localButton1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				Intent localIntent = new Intent("android.intent.action.SENDTO",
						Uri.parse("smsto:"));
				localIntent
						.putExtra("sms_body",
								"一个找车源的工具，爱车行下载地址：http://www.ichehang.com/search/default.aspx");
				MoreShareActivity.this.startActivity(localIntent);
			}
		});
		Button localButton2 = (Button) findViewById(R.id.shareButton02);
		localButton2.setText("通过微信推荐“爱车行”给朋友");
		localButton2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				if (!MoreShareActivity.this.api.isWXAppInstalled()) {
					Toast.makeText(MoreShareActivity.this, "该功能需要安装微信",
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (!MoreShareActivity.this.api.isWXAppSupportAPI()) {
					Toast.makeText(MoreShareActivity.this,
							"您的微信版本不支持该功能，请升级微信。", Toast.LENGTH_SHORT).show();
					return;
				}
				new Thread(MoreShareActivity.this.runnable).start();
			}
		});
		Button localButton3 = (Button) findViewById(R.id.shareButton03);
		localButton3.setText("二维码扫描下载“爱车行”");
		localButton3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				Intent localIntent = new Intent(MoreShareActivity.this,
						MoreShare2CodeActivity.class);
				MoreShareActivity.this.startActivity(localIntent);
			}
		});
	}

}
